package com.octnote.service;

import com.octnote.domain.note.NoteCreateForm;
import com.octnote.domain.note.UserNote;
import com.octnote.repository.NoteRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * 记事服务接口实现
 * Created by leohehe on 2017/4/13.
 */
@Service
public class NoteServiceImpl implements NoteService {
    private NoteRepository noteRepository;
    @Autowired
    private Environment env;

    @Override
    public UserNote create(NoteCreateForm form, HttpServletRequest request) {
        int noteType = 0;
        UserNote userNote  = new UserNote();
        userNote.createNote(form.getNoteContent(),form.getWarnMode(),noteType,form.getMajorNote());
        userNote = noteRepository.save(userNote);
        return userNote;
    }

    @Override
        public UserNote modify(NoteCreateForm form, HttpServletRequest request) {
        //TODO 根据WarnMode重新计算记事状态
        int noteType = 0;
        UserNote userNote  = new UserNote();
        userNote.createNote(form.getNoteContent(),form.getWarnMode(),noteType,form.getMajorNote());
        userNote.setId(Long.parseLong(form.getUserId()));
        noteRepository.save(userNote);
        return userNote;
    }

    @Override
    public Page<UserNote> getNoteList(String searchPhrase, Pageable p) {

        Page<UserNote> userNotes = noteRepository.findByNoteTypeContaining(searchPhrase,p);
        return userNotes;
    }

    public UserNote uploadMultiFile(){
//        //TODO 将多媒体内容保存到服务器的磁盘上
//        MultipartFile[] files = form.getMultipartFile();
//        String multipartFileUri = "";
//        String voiceUri = "";
//        int noteType = 0;
//        UserNote userNote  = new UserNote();
//        userNote.createNote(form.getNoteContent(),multipartFileUri,voiceUri,form.getWarnMode(),noteType,form.getMajorNote());
//        userNote = noteRepository.save(userNote);
//        if(files.length > 0){
//            try {
//                long note_id = userNote.getId();
//                long user_id = userNote.getUserId();
//                // 当前app根目录
//                String rootPath = request.getServletContext().getRealPath("/");
//                // 需要上传的相对地址（application.properties中获取）
//                String relativePath = env.getProperty("multipartfile.upload.dir") + File.separator + user_id +  File.separator + note_id;
//                //根据记事ID创建文件保存的目录
//                File dir = new File(rootPath + File.separator + relativePath);
//                if (!dir.exists())
//                    dir.mkdirs();
//                StringBuilder relativeFile = new StringBuilder();
////                String serverPath = new URL(request.getScheme(), request.getServerName(), request.getServerPort(),
////                        request.getContextPath()).toString();
//                //将文件保存到服务器上
//                for (MultipartFile file : files) {
//                    //获取文件的相对路径
//                    relativeFile.append(relativePath + File.separator + file.getOriginalFilename()).append(",");
//                    file.transferTo(dir);
//                }
//                //更新此记事的imageUri字段
//                userNote.setMultiFileUri(relativeFile.toString());
//                noteRepository.updateUserNoteFileUri(userNote.getId(), userNote.getMultiFileUri());
//            }catch (Exception ex){
//                ex.printStackTrace();
//            }
//        }
//        return userNote;
        return null;
    }

    @Override
    public void delete(Long id) {
        noteRepository.delete(id);
    }
}
